import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);

//引入菜单管理的Vuex
import menu from "./modules/menu";
//引入角色管理的Vuex
import role from './modules/role';
//引入管理员管理的Vuex
import user from './modules/user';
//引入商品分类的Vuex
import cate from './modules/cate';
//引入商品规格的Vuex
import specs from "./modules/specs";
//引入商品管理的Vuex
import goods from './modules/goods';
//引入会员管理的Vuex
import member from './modules/member';
//引入轮播图管理的Vuex
import banner from './modules/banner';
//引入限时秒杀的Vuex
import seckill from './modules/seckill';


//引入登录接口
import { userLogin } from "../request/api";
import { Message } from 'element-ui';
export default new Vuex.Store({
  state: {
    userInfo: sessionStorage.getItem('userInfo') ? JSON.parse(sessionStorage.getItem('userInfo')) : []
  },
  getters: {
  },
  mutations: {
    changeUserInfo(state, payload) {
      //解决Vuex刷新 储存消失的bug
      if (payload) {
        sessionStorage.setItem('userInfo', JSON.stringify(payload));
      } else {
        sessionStorage.removeItem('userInfo');
      }
    }
  },
  actions: {
    loginUser({ commit }, payload) {
      userLogin(payload).then(res => {
        if (res.code == 200) {
          commit('changeUserInfo', res.list);
          //成功提示
          Message.success(res.msg);
        } else {
          Message.error(res.msg);
        }
      })
    }
  },
  modules: {
    menu, role, user, cate, specs, goods,banner,member,seckill
  }
});
